from game.gamesrc.objects.object import MyObject
from game.gamesrc.objects.thing import Thing

#
#Open-Close objects
#

class Lock(MyObject):
    """
    This object is a lock. It's not intented to be used alone but in
    combination with other objects using multiple class inheriting.
    Basically you can add a lock on every object making treasure chests,
    doors, etc.
    The attribute keycode must be the same on the container and the key
    otherwise the lock will not open. A keycode = 0 means the object can
    be closed but not locked with a key.
    """

    def at_object_creation(self):
        super(Lock, self).at_object_creation()
        self.db.is_closed = True
        self.db.is_locked = False
        self.db.keycode = 0
        self.db.flags.append('LOCK')

    def open(self):
        if not self.db.is_closed:
            return
        self.db.is_closed = False

    def close(self):
        if self.db.is_closed:
            return
        self.db.is_closed = True

    def lock(self):
        if self.db.is_locked:
            return
        self.db.is_locked = True

    def unlock(self):
        if not self.db.is_locked:
            return
        self.db.is_locked = False


class Key(Thing):
    """
    This is the key.
    """
    def at_object_creation(self):
        super(Key, self).at_object_creation()
        self.db.flags.append('KEY')
        self.keycode = 1

